Skip to main content

Complete Java Interview Preparation

🎯 Overview​

This comprehensive guide covers everything from core Java concepts to system design problems commonly asked in technical interviews for Java developer positions.


πŸ“š Part 1: Core Java Fundamentals​

1.1 Object-Oriented Programming (OOP)​

Key Concepts:

  • Encapsulation: Bundling data and methods that operate on that data within a single unit (class), hiding internal state and requiring interaction through methods.
  • Inheritance: Mechanism where a new class derives properties and behaviors from an existing class.
  • Polymorphism: Ability of objects to take multiple forms. Includes compile-time (method overloading) and runtime (method overriding).
  • Abstraction: Hiding complex implementation details and showing only essential features.

Critical Interview Questions:

  • Abstract Class vs Interface: Abstract classes can have constructors, state, and method implementations. Interfaces (pre-Java 8) could only have abstract methods. Post-Java 8, interfaces can have default and static methods.
  • Multiple Inheritance: Java doesn't support multiple class inheritance but achieves it through interfaces.
  • Diamond Problem: Resolved using default methods in interfaces with explicit method selection.

1.2 Access Modifiers​

ModifierClassPackageSubclassWorld
publicβœ“βœ“βœ“βœ“
protectedβœ“βœ“βœ“βœ—
defaultβœ“βœ“βœ—βœ—
privateβœ“βœ—βœ—βœ—

Interview Focus: Difference between protected and default (package-private).

1.3 Keywords Deep Dive​

static:

  • Belongs to class, not instance
  • Static methods cannot access instance variables
  • Cannot be overridden (can be hidden)
  • Static blocks execute when class is loaded

final:

  • Variables: Value cannot change (constant)
  • Methods: Cannot be overridden
  • Classes: Cannot be extended
  • final vs finally vs finalize(): final is keyword, finally is block in exception handling, finalize() is method called by GC before object destruction (deprecated in Java 9+)

this vs super:

  • this: Reference to current object
  • super: Reference to parent class object

🧠 Part 2: Java Memory & JVM Internals​

2.1 JVM Architecture​

Components:

  1. Class Loader Subsystem: Bootstrap, Extension, Application class loaders
  2. Runtime Data Areas: Method Area, Heap, Stack, PC Registers, Native Method Stack
  3. Execution Engine: Interpreter, JIT Compiler, Garbage Collector

2.2 Memory Management​

Heap vs Stack:

  • Heap: Stores objects and instance variables. Shared across threads. Slower access.
  • Stack: Stores method calls, local variables, references. Thread-specific. Faster access.

Garbage Collection:

  • Types: Minor GC (Young Generation), Major GC (Old Generation), Full GC
  • Algorithms: Mark and Sweep, Generational GC
  • GC Types: Serial, Parallel, CMS, G1GC, ZGC
  • Reference Types: Strong, Soft, Weak, Phantom

Common Questions:

  • How to prevent memory leaks? Close resources, avoid static collections holding references, use weak references when appropriate.
  • When does OutOfMemoryError occur? Heap space exhaustion, PermGen/Metaspace issues.

⚠️ Part 3: Exception Handling​

Exception Hierarchy​

Throwable
β”œβ”€β”€ Error (OutOfMemoryError, StackOverflowError)
└── Exception
β”œβ”€β”€ RuntimeException (Unchecked)
β”‚ β”œβ”€β”€ NullPointerException
β”‚ β”œβ”€β”€ ArrayIndexOutOfBoundsException
β”‚ └── IllegalArgumentException
└── Checked Exceptions
β”œβ”€β”€ IOException
β”œβ”€β”€ SQLException
└── ClassNotFoundException

Key Points:

  • Checked: Must be caught or declared (compile-time check)
  • Unchecked: Runtime exceptions, no mandatory handling
  • finally: Always executes except System.exit() or JVM crash
  • try-with-resources: Auto-closes resources implementing AutoCloseable

Interview Questions:

  • Can we have try without catch? Yes, with finally or try-with-resources.
  • What happens if exception thrown in finally? Overrides exception from try/catch.

🧡 Part 4: Multithreading & Concurrency​

4.1 Thread Lifecycle​

New β†’ Runnable β†’ Running β†’ Blocked/Waiting/Timed Waiting β†’ Terminated

4.2 Thread Creation​

  1. Extend Thread class
  2. Implement Runnable interface (preferred)
  3. Implement Callable interface (returns value)
  4. Use Lambda expressions with Runnable

4.3 Synchronization​

synchronized keyword:

  • Method level: Locks entire object
  • Block level: Locks specific object/code section

volatile:

  • Ensures visibility of changes across threads
  • No atomicity guarantee for compound operations
  • Lighter than synchronization

Atomic Classes:

  • AtomicInteger, AtomicLong, AtomicBoolean
  • Use CAS (Compare-And-Swap) operations

4.4 Executor Framework​

ExecutorService executor = Executors.newFixedThreadPool(10);
Future<Result> future = executor.submit(callable);

Types:

  • FixedThreadPool
  • CachedThreadPool
  • ScheduledThreadPool
  • SingleThreadExecutor

4.5 Advanced Concurrency​

wait(), notify(), notifyAll():

  • Must be called within synchronized context
  • wait() releases lock, sleep() doesn't

Deadlock Prevention:

  • Lock ordering
  • Lock timeout
  • Deadlock detection

Common Problems:

  • Producer-Consumer (using BlockingQueue)
  • Reader-Writer problem
  • Dining Philosophers

πŸ“¦ Part 5: Collections Framework​

5.1 Collection Hierarchy​

Collection
β”œβ”€β”€ List (ArrayList, LinkedList, Vector)
β”œβ”€β”€ Set (HashSet, LinkedHashSet, TreeSet)
└── Queue (PriorityQueue, ArrayDeque)

Map (HashMap, LinkedHashMap, TreeMap, Hashtable, ConcurrentHashMap)

5.2 Key Comparisons​

FeatureArrayListLinkedList
AccessO(1)O(n)
Insert/Delete (middle)O(n)O(1)
MemoryLessMore
FeatureHashMapHashtableConcurrentHashMap
Thread-safeNoYesYes
Null key1NoNo
Null valueYesNoNo
PerformanceFastSlowFast

5.3 Important Concepts​

equals() and hashCode():

  • If equals() returns true, hashCode() must be same
  • Used in HashMap, HashSet
  • Contract: Equal objects must have equal hash codes

fail-fast vs fail-safe:

  • fail-fast: Throws ConcurrentModificationException (ArrayList, HashMap)
  • fail-safe: Works on clone (CopyOnWriteArrayList, ConcurrentHashMap)

Comparable vs Comparator:

  • Comparable: Natural ordering (compareTo method)
  • Comparator: Custom ordering (compare method)

πŸš€ Part 6: Java 8+ Features​

6.1 Lambda Expressions​

// Before Java 8
Runnable r = new Runnable() {
public void run() { System.out.println("Hello"); }
};

// Java 8+
Runnable r = () -> System.out.println("Hello");

6.2 Functional Interfaces​

  • Predicate<T>: Test condition, returns boolean
  • Function<T, R>: Transform T to R
  • Consumer<T>: Accepts input, no return
  • Supplier<T>: Provides value, no input
  • BiFunction<T, U, R>: Two inputs, one output

6.3 Stream API​

Common Operations:

// map: Transform elements
list.stream().map(String::toUpperCase)

// filter: Select elements
list.stream().filter(s -> s.length() > 5)

// flatMap: Flatten nested structures
list.stream().flatMap(Collection::stream)

// reduce: Aggregate to single value
list.stream().reduce(0, Integer::sum)

// collect: Mutable reduction
list.stream().collect(Collectors.toList())

Intermediate vs Terminal:

  • Intermediate: Lazy, return Stream (map, filter, sorted)
  • Terminal: Trigger execution (collect, forEach, reduce)

6.4 Optional Class​

Optional<String> optional = Optional.ofNullable(value);
optional.ifPresent(System.out::println);
String result = optional.orElse("default");
String result = optional.orElseGet(() -> computeDefault());

6.5 Method References​

  • Static: ClassName::staticMethod
  • Instance: instance::instanceMethod
  • Constructor: ClassName::new

6.6 Default and Static Methods in Interfaces​

Allow adding methods to interfaces without breaking implementations.

6.7 New Features (Java 9+)​

Records (Java 14):

record Person(String name, int age) {}
// Auto-generates constructor, getters, equals, hashCode, toString

Sealed Classes (Java 17):

sealed class Shape permits Circle, Rectangle {}

Text Blocks (Java 15):

String json = """
{
"name": "John"
}
""";

🎨 Part 7: Design Patterns​

7.1 Creational Patterns​

Singleton:

// Thread-safe lazy initialization
public class Singleton {
private static volatile Singleton instance;

private Singleton() {}

public static Singleton getInstance() {
if (instance == null) {
synchronized (Singleton.class) {
if (instance == null) {
instance = new Singleton();
}
}
}
return instance;
}
}

Factory Pattern: Creates objects without specifying exact class.

Builder Pattern: Constructs complex objects step by step.

Prototype Pattern: Creates objects by cloning existing ones.

7.2 Structural Patterns​

Adapter: Converts interface to another expected by client Decorator: Adds functionality to objects dynamically Proxy: Provides surrogate/placeholder for another object Facade: Simplified interface to complex subsystem

7.3 Behavioral Patterns​

Observer: One-to-many dependency (event listeners) Strategy: Family of algorithms, interchangeable Template Method: Skeleton algorithm, steps overridden Command: Encapsulates request as object

7.4 SOLID Principles​

  1. Single Responsibility: Class should have one reason to change
  2. Open/Closed: Open for extension, closed for modification
  3. Liskov Substitution: Subtypes must be substitutable for base types
  4. Interface Segregation: Many specific interfaces better than one general
  5. Dependency Inversion: Depend on abstractions, not concretions

πŸ”§ Part 8: Advanced Java Topics​

8.1 Serialization​

// Serializable marker interface
class User implements Serializable {
private static final long serialVersionUID = 1L;
private String name;
private transient String password; // Not serialized
}

Externalization: Full control over serialization process

8.2 Reflection API​

Class<?> clazz = Class.forName("com.example.MyClass");
Method method = clazz.getDeclaredMethod("methodName");
method.setAccessible(true);
method.invoke(instance, args);

Use Cases: Frameworks, testing, annotations processing

8.3 Annotations​

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface CustomAnnotation {
String value() default "";
}

8.4 Generics​

public <T extends Comparable<T>> T findMax(List<T> list)

Type Erasure: Generic type information removed at runtime

Wildcards:

  • <?>: Unbounded wildcard
  • <? extends T>: Upper bounded (read)
  • <? super T>: Lower bounded (write)

8.5 Java Modules (JPMS - Java 9)​

module com.example.app {
requires java.sql;
exports com.example.api;
}

πŸ’» Part 9: Coding Problems (Practice)​

String Manipulation​

  1. Reverse words in a sentence
  2. Check if string is palindrome
  3. Find first non-repeated character
  4. Remove duplicate characters
  5. Check if two strings are anagrams
  6. Count occurrences of each character

Arrays & Collections​

  1. Find second largest element
  2. Remove duplicates from sorted/unsorted array
  3. Rotate array by k positions
  4. Find missing number in array
  5. Merge two sorted arrays
  6. Group anagrams together

HashMap/Set Problems​

  1. Two sum problem
  2. Find frequency of elements
  3. Longest substring without repeating characters
  4. Group elements by frequency

Multithreading​

  1. Print even/odd numbers using two threads
  2. Producer-Consumer problem
  3. Print sequence using three threads
  4. Implement thread-safe singleton

Java 8 Streams​

  1. Find frequency of each element
  2. Sort list of objects by multiple fields
  3. Partition list into even/odd numbers
  4. FlatMap nested lists
  5. Find top N elements
  6. Custom collectors

πŸ—οΈ Part 10: Low-Level Design (LLD)​

LLD Problem-Solving Approach​

  1. Clarify Requirements: Ask questions about scope, constraints
  2. Identify Core Entities: Classes, relationships
  3. Define Relationships: Inheritance, composition, aggregation
  4. Apply Design Patterns: Where appropriate
  5. Consider SOLID Principles
  6. Handle Edge Cases

Common LLD Problems​

10.1 Parking Lot System​

Requirements:

  • Multiple floors, spots of different sizes
  • Vehicle types: Car, Bike, Truck
  • Entry/exit with ticket
  • Payment calculation

Key Classes:

  • ParkingLot (Singleton)
  • Floor, ParkingSpot (Abstract)
  • Vehicle (Abstract)
  • Ticket, Payment
  • ParkingStrategy (Strategy pattern)

Design Considerations:

  • Use Factory for vehicle creation
  • Strategy pattern for spot allocation
  • Observer pattern for spot availability

10.2 Elevator System​

Requirements:

  • Multiple elevators in building
  • Up/down requests from floors
  • Optimal elevator assignment
  • Door operations

Key Classes:

  • ElevatorController
  • Elevator, Door, Button
  • Request (External/Internal)
  • ElevatorSelectionStrategy

Algorithms:

  • SCAN (elevator algorithm)
  • LOOK algorithm
  • Shortest Seek Time First

10.3 Library Management System​

Requirements:

  • Books, members, librarians
  • Issue/return books
  • Fine calculation
  • Search functionality

Key Classes:

  • Library (Facade)
  • Book, Member, Librarian
  • Catalog (search)
  • Transaction, Fine

10.4 Hotel Booking System​

Requirements:

  • Search rooms by criteria
  • Book/cancel reservations
  • Room types, pricing
  • Customer management

Key Classes:

  • Hotel, Room (types)
  • Booking, Customer
  • SearchCriteria, RoomSearchService
  • PaymentService

10.5 ATM Machine​

Requirements:

  • Authentication
  • Check balance, withdraw, deposit
  • Cash dispenser management
  • Transaction logging

Key Classes:

  • ATM, Card, Account
  • CashDispenser
  • Transaction (types)
  • AuthenticationService

Design Patterns:

  • State pattern for ATM states
  • Chain of Responsibility for cash dispensing

10.6 Online Shopping System (E-commerce)​

Key Classes:

  • User, Product, Cart, Order
  • Inventory, Payment, Shipping
  • SearchService, RecommendationEngine

10.7 Chess Game​

Requirements:

  • Board setup, piece movements
  • Turn management
  • Check/checkmate detection
  • Move validation

Key Classes:

  • Board, Cell
  • Piece (abstract), specific pieces
  • Player, Game
  • MoveValidator

10.8 Logging Framework​

Requirements:

  • Multiple log levels
  • Different outputs (console, file)
  • Thread-safe
  • Configurable format

Key Classes:

  • Logger (Singleton)
  • LogLevel (enum)
  • Appender (interface) - Console, File
  • Formatter

Patterns: Singleton, Strategy, Template Method

10.9 Notification Service​

Requirements:

  • Multiple channels (Email, SMS, Push)
  • Priority handling
  • Retry mechanism
  • Rate limiting

Key Classes:

  • NotificationService
  • NotificationChannel (Strategy)
  • NotificationQueue
  • RetryHandler

10.10 Ride-Sharing System (Uber/Ola)​

Key Classes:

  • Driver, Rider, Ride
  • Location, Route
  • PricingStrategy
  • MatchingService

🌐 Part 11: High-Level Design (HLD)​

HLD Approach​

  1. Requirements Gathering: Functional & Non-functional
  2. Capacity Estimation: Users, requests, storage
  3. API Design: Endpoints, request/response
  4. Data Model: Database schema
  5. High-Level Components: Services, communication
  6. Scalability & Performance: Caching, load balancing
  7. Reliability: Replication, backup
  8. Security: Authentication, authorization

Common HLD Problems​

11.1 URL Shortener (TinyURL)​

Functional Requirements:

  • Shorten long URL
  • Redirect to original URL
  • Custom aliases
  • Analytics (optional)

Non-Functional:

  • Low latency, high availability
  • 100M URLs/day

Design:

  • Encoding: Base62 (a-z, A-Z, 0-9)
  • Database: NoSQL (key-value), sharded
  • Caching: Redis for popular URLs
  • API:
    • POST /shorten β†’{shortUrl}
    • GET /{shortCode} β†’ Redirect

Components:

  • Load Balancer
  • Application Servers
  • Database (sharded by hash)
  • Cache Layer
  • Analytics Service

11.2 Design Twitter​

Functional:

  • Tweet, follow/unfollow
  • Timeline (home, user)
  • Search tweets

Non-Functional:

  • 500M users, 200M DAU
  • Handle celebrity problem

Design:

  • Data Model: User, Tweet, Follow tables
  • Timeline Generation:
    • Fan-out on write (pre-compute)
    • Fan-out on read (compute on demand)
    • Hybrid approach
  • Components:
    • Tweet Service
    • Timeline Service
    • Notification Service
    • Search Service (Elasticsearch)

Technologies:

  • Redis for timeline cache
  • Kafka for event streaming
  • Cassandra for tweets
  • PostgreSQL for users

11.3 Design WhatsApp/Chat System​

Functional:

  • One-on-one messaging
  • Group chat
  • Sent/delivered/read receipts
  • Media sharing

Non-Functional:

  • Real-time delivery
  • High availability
  • End-to-end encryption

Design:

  • Protocol: WebSocket for real-time
  • Message Queue: Kafka/RabbitMQ
  • Database:
    • User data: SQL
    • Messages: Cassandra (wide column)
  • Storage: S3 for media
  • Components:
    • Connection Service (WebSocket)
    • Message Service
    • Group Service
    • Notification Service

11.4 Design Netflix/YouTube​

Functional:

  • Upload/stream videos
  • Search, recommendations
  • User profiles, subscriptions

Non-Functional:

  • Low latency streaming
  • High bandwidth
  • Global availability

Design:

  • CDN: Store videos closer to users
  • Encoding: Multiple resolutions
  • Adaptive Streaming: HLS/DASH
  • Components:
    • Upload Service (chunked upload)
    • Encoding Service (queue-based)
    • CDN (CloudFront, Akamai)
    • Recommendation Engine (ML)
    • Metadata DB (PostgreSQL)

11.5 Design Uber/Cab Booking​

Functional:

  • Find nearby drivers
  • Book ride, track location
  • Payment, rating

Design:

  • Location Service:
    • QuadTree for spatial indexing
    • Geohashing
  • Matching Algorithm:
    • Priority queue
    • Proximity-based
  • Components:
    • Location Service
    • Matching Service
    • Ride Service
    • Payment Service
    • Notification Service

Databases:

  • PostgreSQL (users, rides)
  • Redis (driver locations)
  • Cassandra (trip history)

11.6 Design Instagram​

Functional:

  • Upload photos, feed
  • Follow users, like/comment
  • Stories

Design:

  • Feed Generation:
    • Fan-out on write for regular users
    • Fan-out on read for celebrities
  • Storage: S3 for images
  • Image Processing: Resize, thumbnails
  • Database:
    • PostgreSQL (users, relationships)
    • Cassandra (posts, comments)

11.7 Design Dropbox/Google Drive​

Functional:

  • Upload/download files
  • Sync across devices
  • Sharing, versioning

Design:

  • Chunking: Break files into chunks
  • Deduplication: Hash-based
  • Sync: Long polling/WebSocket
  • Storage:
    • Metadata DB (MySQL)
    • Block storage (S3)
  • Components:
    • Upload/Download Service
    • Sync Service
    • Notification Service

11.8 Design Rate Limiter​

Algorithms:

  • Token Bucket
  • Leaky Bucket
  • Fixed Window Counter
  • Sliding Window Log
  • Sliding Window Counter

Implementation:

  • Redis with TTL
  • In-memory cache
  • Distributed rate limiting

11.9 Design Distributed Cache​

Requirements:

  • Get/Set operations
  • Eviction policies (LRU, LFU)
  • High availability

Design:

  • Consistent Hashing: Distribute keys
  • Replication: Master-slave
  • Eviction: LRU using LinkedHashMap
  • Sharding: Horizontal partitioning

11.10 Design Payment System​

Functional:

  • Process payments
  • Handle multiple payment methods
  • Refunds, transaction history

Non-Functional:

  • ACID properties
  • PCI DSS compliance
  • Idempotency

Design:

  • Database: PostgreSQL with ACID
  • State Machine: Payment states
  • Idempotency Keys: Prevent duplicate charges
  • Components:
    • Payment Gateway Integration
    • Transaction Service
    • Reconciliation Service
    • Notification Service

πŸ“Š Part 12: System Design Concepts​

12.1 Scalability Patterns​

  • Vertical Scaling: Increase server capacity
  • Horizontal Scaling: Add more servers
  • Load Balancing: Distribute traffic
  • Caching: Reduce database load
  • CDN: Serve static content
  • Database Sharding: Partition data
  • Microservices: Decompose monolith

12.2 Database Concepts​

  • SQL vs NoSQL: ACID vs BASE
  • Replication: Master-slave, Master-master
  • Sharding Strategies: Range, Hash, Directory
  • Indexes: B-Tree, Hash, Full-text
  • Normalization vs Denormalization
  • CAP Theorem: Consistency, Availability, Partition Tolerance

12.3 Caching Strategies​

  • Cache-Aside: Load data on cache miss
  • Write-Through: Write to cache and DB simultaneously
  • Write-Back: Write to cache, async to DB
  • Refresh-Ahead: Preload data before expiry

12.4 Communication Patterns​

  • REST: Stateless, resource-based
  • GraphQL: Flexible queries
  • gRPC: High-performance RPC
  • WebSocket: Bidirectional real-time
  • Message Queues: Async communication

12.5 Distributed Systems Concepts​

  • Consistency Models: Strong, Eventual, Causal
  • Consensus Algorithms: Paxos, Raft
  • Distributed Transactions: 2PC, Saga pattern
  • Service Discovery: Eureka, Consul
  • Circuit Breaker: Prevent cascading failures

πŸ“ Part 13: Interview Tips​

Technical Interview Strategy​

  1. Clarify Requirements: Ask questions before coding
  2. Think Aloud: Explain your thought process
  3. Start Simple: Basic solution, then optimize
  4. Consider Edge Cases: Null, empty, boundaries
  5. Analyze Complexity: Time and space
  6. Test Your Code: Walk through with examples

Common Mistakes to Avoid​

  • Jumping to code without understanding problem
  • Not handling null/edge cases
  • Poor variable naming
  • Ignoring time/space complexity
  • Not testing solution

Preparation Strategy​

  1. Week 1-2: Core Java, OOP, Collections
  2. Week 3: Multithreading, Java 8+ features
  3. Week 4: Design patterns, SOLID principles
  4. Week 5-6: LLD problems (practice 2-3/day)
  5. Week 7-8: HLD problems, system design concepts
  6. Week 9-10: Coding practice (LeetCode/HackerRank)
  7. Week 11-12: Mock interviews, revision

Resources​

  • Books: Effective Java, Head First Design Patterns
  • Coding: LeetCode, HackerRank, CodeSignal
  • System Design: Grokking System Design, SystemDesignPrimer
  • LLD: GitHub repositories, YouTube channels

βœ… Final Checklist​

Before Interview​

  • Review core Java concepts
  • Practice 5-10 coding problems
  • Review 2-3 design patterns
  • Prepare 1 LLD and 1 HLD problem
  • Know your resume projects deeply
  • Prepare questions to ask interviewer

During Interview​

  • Listen carefully to requirements
  • Ask clarifying questions
  • Think before coding
  • Communicate your approach
  • Handle feedback gracefully
  • Stay calm and confident

Key Takeaway​

Master the fundamentals first, then move to advanced topics. Practice consistently, and don't just memorizeβ€”understand the 'why' behind every concept.

Good luck with your interviews! πŸš€